const util = {
    extend(src1, src2) {
        const src1Type = this.getType(src1);
        const src2Type = this.getType(src2);
        if (src1Type !== 'object' || src2Type !== 'object') {
            return {};
        }
        const target = JSON.parse(JSON.stringify(src1));
        let keyType;
        const keys = Object.keys(src2);
        keys.forEach((key) => {
            keyType = this.getType(src2[key]);
            if (keyType === 'object') {
                if (!target[key]) {
                    target[key] = JSON.parse(JSON.stringify(src2[key]));
                } else {
                    target[key] = this.extend(target[key], src2[key]);
                }
            } else {
                target[key] = src2[key];
            }
        });
        return target;
    },
    // 获取变量类型
    getType(variety) {
        const map = {
            '[object Boolean]': 'boolean',
            '[object Number]': 'number',
            '[object String]': 'string',
            '[object Function]': 'function',
            '[object Array]': 'array',
            '[object Date]': 'date',
            '[object RegExp]': 'regExp',
            '[object Undefined]': 'undefined',
            '[object Null]': 'null',
            '[object Object]': 'object',
            '[object Arguments]': 'arguments',
            '[object HTMLCanvasElement]': 'canvas',
        };
        // if (typeof window !== 'undefined') {
        //     if (variety instanceof Element) {
        //         return 'Element';
        //     }
        // }
        return map[Object.prototype.toString.call(variety)];
    },
};

export default util;
